package com.zzx.magic.controller;

import cn.hutool.extra.qrcode.QrCodeUtil;
import com.alibaba.fastjson.JSONObject;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.payment.common.models.AlipayTradeQueryResponse;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author Zhangxx
 * @Date 2024/6/20 17:04
 */
@RestController
@Slf4j
public class AliPayController {

    @Resource
    private Config config;

    @PostMapping("/pay")
    public void pay(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Factory.setOptions(config);
        AlipayTradePrecreateResponse payResponse = Factory.Payment.FaceToFace().preCreate("订单主题: 产融通", "info123", "123");
        String httpBody = payResponse.getHttpBody();
        JSONObject jsonObject = JSONObject.parseObject(httpBody);
        String qrCode = jsonObject.getJSONObject("alipay_trade_precreate_response").getString("qr_code");
        QrCodeUtil.generate(qrCode, 300,300,"png", response.getOutputStream());

    }

    @PostMapping("/notify")
    public void notify(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HashMap<String, String> params = new HashMap<>();
        Map<String, String[]> parameterMap = request.getParameterMap();
        for (String name:parameterMap.keySet()) {
            String[] strings = parameterMap.get(name);
            String valueStr = "";
            for (int i = 0; i < strings.length; i++) {
                valueStr = (i == strings.length - 1) ? valueStr + strings[i]
                        : valueStr + strings[i] + ",";
            }
            valueStr= new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
        Boolean signResult = Factory.Payment.Common().verifyNotify(params);
        if(signResult) {
            log.info("收到支付宝发送的支付结果通知");
            String out_trade_no = request.getParameter("out_trade_no");
            log.info("交易流水号:{}", out_trade_no);
            //交易状态
            String trade_status = new String(request.getParameter("trade_status")
                    .getBytes("ISO-8859-1"),
                    "utf-8");
            switch (trade_status) {
                case "TRADE_SUCCESS" :
                    log.info("");
                    break;
                case "TRADE_FINISHED" :
                    log.info("");
                    break;
                case "TRADE_CLOSED":
                    log.info("");
                    break;
                case "WAIT_BUYER_PAY":
                    log.info("交易创建等待买家付款");
                    break;

            }
            response.getWriter().write("sucess");
        } else {
            response.getWriter().write("fail");
        }
    }

    @GetMapping("/query")
    public String query() throws Exception {
        Factory.setOptions(config);
        AlipayTradeQueryResponse response = Factory.Payment.Common().query("da");
        return response.getHttpBody();
    }


}